<template>
  <el-dialog :title="titleText" :visible="showDialog" width="500px" @close="close">
    <el-form ref="addForm" label-width="120px" :model="deptForm" :rules="deptRules">
      <el-form-item label="部门名称" size="mini" prop="name">
        <el-input v-model="deptForm.name" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" size="mini" prop="code">
        <el-input v-model="deptForm.code" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" size="mini" prop="managerId">
        <el-select v-model="deptForm.managerId" placeholder="请选择部门负责人">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" size="mini" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getDepartmentList, getManagerList, addDepartment, getDepartmentDetail, updateDepartment } from '@/api/department'
export default ({
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNode: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: '',
      deptForm: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      deptRules: {
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' }, { min: 2, max: 10, message: '部门编码长度在2-10位字符之间', trigger: 'blur' }, { trigger: 'blur', validator: async(rule, value, callback) => {
          let list = await getDepartmentList()
          // 如果表单中有id，代表当前是编辑模式，所有在判断部门编号是否重复时需要先排除掉自身
          if (this.deptForm.id) {
            list = list.filter(item => item.id !== this.deptForm.id)
          }
          if (list.some(item => item.code === value)) {
            callback(new Error('部门编号已存在'))
          } else {
            callback()
          }
        } }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, { min: 2, max: 10, message: '部门介绍长度在2-10位字符之间', trigger: 'blur' }],
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }, { min: 1, max: 100, message: '部门名称长度在1-100位字符之间', trigger: 'blur' }, { trigger: 'blur', validator: async(rule, value, callback) => {
          let list = await getDepartmentList()
          // 如果表单中有id，代表当前是编辑模式，所有在判断部门名称是否重复时需要先排除掉自身
          if (this.deptForm.id) {
            list = list.filter(item => item.id !== this.deptForm.id)
          }
          if (list.some(item => item.name === value)) {
            callback(new Error('部门名称已存在'))
          } else {
            callback()
          }
        } }]
      }
    }
  },
  computed: {
    titleText() { return this.deptForm.id ? '编辑部门' : '新增部门' }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      // 手动重置清除id
      this.deptForm = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      //   重置表单
      this.$refs.addForm.resetFields()
      //   关闭弹窗
      this.$emit('update:showDialog')
    },
    async getManagerList() {
      this.managerList = await getManagerList()
    },
    async btnOK() {
      this.$refs.addForm.validate(async isOK => {
        if (isOK) {
          let msg
          if (this.deptForm.id) {
            msg = '更新部门成功'
            await updateDepartment(this.deptForm)
          } else {
            await addDepartment({ ...this.deptForm, pid: this.currentNode })
            msg = '添加部门成功'
          }
          //   通知父组件重新获取部门列表
          this.$emit('updateDepartmentList')
          this.close()
          this.$message.success(msg)
        }
      })
    },
    async getDepartmentDetail() {
      this.deptForm = await getDepartmentDetail(this.currentNode)
    }
  }
})
</script>
